package com.jasonzqshen.ifm.featureModelCore.shapes;

import org.w3c.dom.Element;

public class FMPoint {
    private static final String XML_POINT_X = "PointX";

    private static final String XML_POINT_Y = "PointY";

    private double x;

    private double y;

    public FMPoint() {
        x = -1;
        y = -1;
    }

    public FMPoint(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public void toXElem(Element elem) {
        elem.setAttribute(XML_POINT_X, ((Double) x).toString());
        elem.setAttribute(XML_POINT_Y, ((Double) y).toString());
    }

    public static FMPoint parseXElem(Element xelem) {
        String xStr = xelem.getAttribute(XML_POINT_X);
        String yStr = xelem.getAttribute(XML_POINT_Y);

        if (xStr.equals("") || yStr.equals("")) {
            return new FMPoint();
        }

        return new FMPoint(Double.parseDouble(xStr), Double.parseDouble(yStr));
    }

    @Override
    public Object clone() {
        return new FMPoint(x, y);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof FMPoint)) {
            return false;
        }
        FMPoint tmpPoint = (FMPoint) obj;
        return this.x == tmpPoint.x && this.y == tmpPoint.y;
    }
}
